package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.AutomobileBrandMapper;
import com.gxa.insurance.mapper.ModelMapper;
import com.gxa.insurance.mapper.SeriesMapper;
import com.gxa.insurance.pojo.AutomobileBrand;
import com.gxa.insurance.pojo.Model;
import com.gxa.insurance.pojo.Series;
import com.gxa.insurance.service.AutomobileBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wang li sha
 * @date 2022-06-06 16:33
 */
@Service("automobileBrandService")
@Transactional(rollbackFor = Throwable.class)
public class AutomobileBrandServiceImpl implements AutomobileBrandService {
    @Autowired
    AutomobileBrandMapper automobileBrandMapper;

    @Autowired
    SeriesMapper seriesMapper;

    @Autowired
    ModelMapper modelMapper;

    @Override
    public Integer insert(AutomobileBrand automobileBrand) throws Exception {

        Integer insert = automobileBrandMapper.insert(automobileBrand);
        if (insert != 1) {
            throw new BusinessException(ExceptionCode.INSERT_AUTOMOBILEBRAND_FAILED);
        }
        return insert;
    }

    @Override
    public Integer update(AutomobileBrand automobileBrand) throws Exception {

        Integer update = automobileBrandMapper.update(automobileBrand);
        if (update != 1) {
            throw new BusinessException(ExceptionCode.UPDATE_FILED);
        }
        return update;
    }


    @Override
    public AutomobileBrand selectById(Integer id) throws Exception {
        AutomobileBrand automobileBrand = automobileBrandMapper.selectById(id);
        if (automobileBrand == null) {
            throw new BusinessException(ExceptionCode.EMPTY_LIST);
        }
        return automobileBrand;
    }

    @Override
    public List<String> selectAll() throws Exception {
        List<String> automobileBrandMappers = automobileBrandMapper.selectAll();
        if (automobileBrandMappers.size() == 0) {
            throw new BusinessException(ExceptionCode.AUTOMOBILEBRAND_IS_EMPTY);
        }
        return automobileBrandMappers;
    }

    @Override
    public List<Series> getSeries(String name) throws Exception {
        List<Series> series = seriesMapper.selectByName(name);
        if (series.size() == 0) {
            throw new BusinessException(ExceptionCode.EMPTY_LIST);
        }
        return series;
    }

    @Override
    public List<Model> getModel(String name) throws Exception {
        List<Model> models = modelMapper.selectByName(name);
        if (models.size() == 0) {
            throw new BusinessException(ExceptionCode.EMPTY_LIST);
        }
        return models;
    }

}
